package com.hisicom.scqxspxcjc.sceneCheck.systemCheck.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.hisicom.scqxspxcjc.base.dao.BaseRepository;
import com.hisicom.scqxspxcjc.base.service.BaseService;
import com.hisicom.scqxspxcjc.common.file.bean.FileBean;
import com.hisicom.scqxspxcjc.common.file.serivce.FileService;
import com.hisicom.scqxspxcjc.sceneCheck.systemCheck.domain.SystemCheck;
import com.hisicom.scqxspxcjc.sceneCheck.systemCheck.exception.SystemCheckException;
import com.hisicom.scqxspxcjc.sceneCheck.systemCheck.repository.SystemCheckRepository;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhonggls
 * @date 2020/12/21 14:12
 * @Explain
 */
@Service
@Slf4j
public class SystemCheckService implements BaseService<SystemCheck, String> {
    @Autowired
    private SystemCheckRepository systemCheckRepository;
 @Autowired
    private FileService fileService;

    @Override
    public BaseRepository<SystemCheck, String> getRepository() {
        return systemCheckRepository;
    }

    public List<SystemCheck> findByAccount(String account) {
        return systemCheckRepository.findByAccount(account);
    }

    public List<SystemCheck> findByAccountAndCheckStatus(String account) {
        return systemCheckRepository.findByAccountAndCheckStatus(account, "0");
    }

    public List<SystemCheck> findByCheckStatus(@NonNull String checkStatus) {
        return systemCheckRepository.findByCheckStatus(checkStatus);
    }

    public void updateCheckStatus(@NonNull String groupId, @NonNull String checkStatus) {
        List<SystemCheck> systemCheckList = findByGroupId(groupId);
        systemCheckList.forEach(systemCheck -> {
            systemCheck.setCheckStatus(checkStatus);
            save(systemCheck);
        });
    }

    public List<SystemCheck> findByGroupId(String groupId) {
        return systemCheckRepository.findByGroupId(groupId);
    }

    public SystemCheck addSyctemCheck(SystemCheck systemCheck) {
        if (ObjectUtil.isEmpty(systemCheck)) {
            throw new SystemCheckException("获取现场检查失败");
        }
        if (StrUtil.isEmpty(systemCheck.getAccount())) {
            throw new SystemCheckException("保存失败，用户名不能为空");
        }
        if (StrUtil.isEmpty(systemCheck.getPassword())) {
            throw new SystemCheckException("保存失败，密码不能为空");
        }
        if (StrUtil.isEmpty(systemCheck.getGroupId())) {
            throw new SystemCheckException("保存失败，分组不能为空");
        }
        if(ObjectUtil.isNotEmpty(systemCheck.getFileBeanList())){
            for(FileBean fileBean:systemCheck.getFileBeanList()){
                fileService.save(fileBean);
            }
        }
        return save(systemCheck);
    }


}
